package com.website.controller;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.website.service.impl.UserServiceImpl;

@Controller
@RequestMapping("/")
public class OfficialWebsiteController {
	
	 @Autowired
	 private UserServiceImpl userService;
	 
	 private static Map<String, Object> eventmap = new Hashtable<>(); 
	
    /**
     * 跳转到主页
     * @return
     */
	@RequestMapping("/")
	public String index(){
		return "index";
	}
	
    /**
     * 跳转到主页
     * @return
     */
	@RequestMapping("/index")
	public String index2(){
		return "index";
	}
	
	/**
	 * 跳转到查询表单
	 * @return
	 */
	@RequestMapping("/formInfo")
	public String formInfo(){
		return "html/formInfo";
	}
	
	/**
	 * 跳转到反馈结果
	 * @return
	 */
	@RequestMapping("/result")
	public String result(HttpServletRequest request,ModelMap model){
		HttpSession session = request.getSession();
		String genderType = (String)session.getAttribute("genderType");
		String familyType = (String)session.getAttribute("familyType");
		String cartype = (String)session.getAttribute("cartype");
		String housetype = (String)session.getAttribute("housetype");
		String familyrevenue = (String)session.getAttribute("familyrevenue");
		
		
		session.removeAttribute("genderType");
		session.removeAttribute("familyType");
		session.removeAttribute("cartype");
		session.removeAttribute("housetype");
		session.removeAttribute("familyrevenue");
		if(cartype==null){
			cartype = "有车";
		}
		if(housetype==null){
			housetype = "有房";
		}
		if(familyType==null){
			familyType = "幸福已婚";
		}
		
		if(familyrevenue==null){
			familyrevenue = "30000";
		}
		
	 Map<String,Object> map = new HashMap<String,Object>();
	 map.put("genderType", genderType);
	 map.put("familyType", familyType);
	 map.put("familyrevenue",familyrevenue);
	 map.put("cartype",cartype);
	 map.put("housetype",housetype);


     Map<String, Object> eventnames = new Hashtable<>();
     eventnames.putAll(eventmap);
	 map.put("eventnames", eventnames);
	 eventmap.clear();
	 userService.addcustomer(map);
	 model.put("familyrevenue", familyrevenue);
	 model.put("cartype", cartype);
	 model.put("housetype", housetype);
		return "html/result";
	}
	
 
	/**
	 * 把用户数据放入session中
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/getinfo")
	@ResponseBody
	public void getinfo(HttpServletRequest request,ModelMap model){
		HttpSession session = request.getSession();
		String type = request.getParameter("type");
		String value = request.getParameter("value");
		session.setAttribute(type, value);
	
	}
	
	/**
	 * 把房车数据放入session中
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/getcarhouseinfo")
	@ResponseBody
	public void getcarhouseinfo(HttpServletRequest request,ModelMap model){
		HttpSession session = request.getSession();
		String cartype = request.getParameter("cartype");
		String housetype = request.getParameter("housetype");
		if(cartype != null){
			session.setAttribute("cartype", cartype);
		}
	if(housetype != null){
		session.setAttribute("housetype", housetype);
	}
		
	}

	
	/**
	 * 把事件数据放入静态的hashtable中
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/geteventinfobytable")
	@ResponseBody
	public void geteventinfobytable(HttpServletRequest request,ModelMap model){
		String eventname = request.getParameter("eventname");
		String count = eventmap.size()+"";
		eventmap.put(count, eventname);
	}
	
	/**
	 * 保险点一个推送一个
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/getinsurance")
	@ResponseBody
	public void getinsurance(HttpServletRequest request,ModelMap model){
		String who = request.getParameter("who");
		String type = request.getParameter("type");
		String money = request.getParameter("money");
		String insurance = ":"+type;
		userService.addInsurance(insurance);
	}
 
}
